package com.wjjun.library.viewmodel

import androidx.annotation.LayoutRes
import androidx.lifecycle.MutableLiveData
import com.wjjun.base.ui.viewmodel.BaseItemViewModel

/**
 *
 * author : wangjunjun
 * date   : 2022/7/26
 * desc   :
 */
class CheckItemViewModel<T> : BaseItemViewModel<T>() {

    /**
     *  是否显示选择
     */
    val showCheck = MutableLiveData(false)

    val checked = MutableLiveData(false)

    val text = MutableLiveData("")

    /**
     * 布局id
     */
    @LayoutRes
    var layoutIdRes = 0

    /**
     * 类型, 同类型的会根据是否单选进行判断
     */
    var group: Long = 0

    /**
     * 是否单选
     */
    var isSingle = false

    /**
     * 是否默认
     */
    var isDef = false

    var span = 1


    override fun getItemLayoutId(): Int {
        return layoutIdRes
    }

    override fun getItemSpanSize(maxSpan: Int): Int {
        return span
    }

    fun onItemClick(list: List<BaseItemViewModel<*>>) {
        if (isSingle) {
            list.forEach {
                if (it !is CheckItemViewModel<*>) {
                    return
                }
                it.checked.value = !(it.isSingle && it.group == this.group && it != this)
            }
        } else {
            checked.value = !(checked.value ?: false)
        }
    }

}
